package com.zhuozhou.jdyc.wight.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.databinding.DialogSpecificationBinding;
import com.zhuozhou.jdyc.main.goods.bean.BasicsCommodityBase;
import com.zhuozhou.jdyc.util.log.ToastUtils;

import java.util.List;

/**
 * Create by Once_ChuXy on 2021/9/15
 * Describe:规格选择
 */
public class SpecificationDialog extends Dialog implements View.OnClickListener {
    private Activity context;
    private List<String> labels;
    private BasicsCommodityBase base;
    private int goodWeight = 1;
    private LatLng latLng;
    private OnDialogInterfaceClickListener sureClickListener;

    public SpecificationDialog(@NonNull Activity context) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
    }

    public SpecificationDialog(@NonNull Activity context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public SpecificationDialog setOnSureClickListener(OnDialogInterfaceClickListener sureClickListener) {
        this.sureClickListener = sureClickListener;
        return this;
    }

    @Override
    public void onClick(View v) {

    }

    public SpecificationDialog setLabels(List<String> labels) {
        this.labels = labels;
        return this;
    }

    public SpecificationDialog setInfo(BasicsCommodityBase base) {
        this.base = base;
        return this;
    }

    public SpecificationDialog builder() {
        init();
        return this;
    }

    private void init() { //规格
        DialogSpecificationBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_specification, null, false);
        setContentView(binding.getRoot());//核心代码
        binding.setSpec(base.getData());
        Glide.with(context).load(base.getData().getSpecData().getSpec_list().get(0).getForm().getImgshow()).into(binding.imageMerchant);
        if (labels != null && labels.size() > 0) {
            binding.labels.setLabels(labels);
        }

        binding.labels.setOnLabelSelectChangeListener((label, data, isSelect, position) -> {
            binding.tvSelected.setText(data.toString());
        });
        binding.imageDismiss.setOnClickListener(v -> {//点击X关闭
            sureClickListener.onDialogClick(labels.get(binding.labels.getSelectLabels().get(0)), 0, goodWeight);
            dismiss();
        });
        binding.tvAddCart.setOnClickListener(v -> {//点击添加购物车
            sureClickListener.onDialogClick(labels.get(binding.labels.getSelectLabels().get(0)), 1, goodWeight);
            dismiss();
        });
        binding.tvBuyNow.setOnClickListener(v -> {//点击立即购买
            sureClickListener.onDialogClick(labels.get(binding.labels.getSelectLabels().get(0)), 2, goodWeight);
            dismiss();
        });
        binding.tvReduce.setOnClickListener(v -> {
            if (goodWeight <= 0) {
                return;
            }
            if (goodWeight == 1) {
                ToastUtils.showShort(context, "该商品不能再减少了哦");
                return;
            }
            goodWeight--;
            binding.tvNum.setText(goodWeight + "");
        });
        binding.tvAdd.setOnClickListener(v -> {
            if (goodWeight <= 0) {
                return;
            }
            if (goodWeight >= 9999) {
                ToastUtils.showShort(context, "该商品不能再增加了哦");
                return;
            }
            goodWeight++;
            binding.tvNum.setText(goodWeight + "");
        });
        Window dialogWindow = getWindow();
        if (dialogWindow == null) {
            return;
        }
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(dm);
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    dm.heightPixels * 2 / 3);
        } else {
            dialogWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    public interface OnDialogInterfaceClickListener {
        void onDialogClick(String specification, Integer type, Integer goodWeight);
    }

}
